package cn.wolfcode.plus.web.controller;

import cn.wolfcode.plus.domain.Department;
import cn.wolfcode.plus.domain.Employee;
import cn.wolfcode.plus.domain.JsonResult;
import cn.wolfcode.plus.domain.Role;
import cn.wolfcode.plus.qo.PageResult;
import cn.wolfcode.plus.qo.QueryObject;
import cn.wolfcode.plus.service.IDepartmentService;
import cn.wolfcode.plus.service.IEmployeeService;
import cn.wolfcode.plus.service.IRoleService;
import cn.wolfcode.plus.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author wyy
 * Date 2021/2/10 11:42
 **/
@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @RequiredPermission(name="员工列表",expression="employee:list")
    @RequestMapping("/list")
    //@ResponseBody
    public String list(Model model, QueryObject qo){
        PageResult<Employee> result = employeeService.query(qo);
        model.addAttribute("result",result);
        List<Department> departments = departmentService.list();
        model.addAttribute("departments",departments);
        return "employee/list";
    }

    @RequiredPermission(name="员工删除",expression="employee:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        employeeService.removeById(id);
        return "redirect:/employee/list";
    }

    @RequiredPermission(name="员工新增/编辑",expression="employee:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id,Model model){
        List<Department> departments = departmentService.list();
        model.addAttribute("departments",departments);
        List<Role> roles = roleService.list();
        model.addAttribute("roles",roles);
        if(id!=null){
            //去修改
            List<Role> rolesForId = employeeService.getAllRoles(id);
            model.addAttribute("rolesForId",rolesForId);
            Employee employee = employeeService.getById(id);
            model.addAttribute("employee",employee);
        }
        return "/employee/input";
    }

    @RequiredPermission(name="员工新增/编辑",expression="employee:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] ids){
        if(employee.getId()==null){
            //执行增加操作
            employeeService.save(employee);
        }else{
            //修改之前-删除该员工关联角色的关系
            employeeService.deleteRelation(employee.getId());
            //执行修改操作
            employeeService.updateById(employee);
        }
        if(ids!=null){
            for (Long id : ids) {
                employeeService.insertRelation(employee.getId(),id);
            }
        }
        JsonResult result = new JsonResult();
        result.setSuccess(true);
        return result;
    }

    @RequestMapping("/checkName")
    @ResponseBody
    public Map checkName(String username){
        HashMap<String, Boolean> map = new HashMap<>();
        Employee employee = employeeService.selectByUsername(username);
        map.put("valid",employee==null);//验证不通过返回false,验证通过返回true
        return map;
    }
}
